package com.yyfax.ph.personal;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
/**
 * 获取公司
 * Created by zhangxian on 2017/11/7.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class PersonCenterPresenter extends Presenter<PersonCenterModel, PersonCenterPresenter.View> {
	private static final String TAG = PersonCenterPresenter.class.getSimpleName();

	public static class Generator implements PresenterFactory<PersonCenterPresenter> {
		@Override
		public PersonCenterPresenter create() {
			return new PersonCenterPresenter();
		}
	}

	public interface View extends IView {
		String UNKNOWN_MONEY_AMOUNT = "--";

		void getBalanceResult(String unWithDrawMoney, String bankCode, String bankCard);

		void getApplyCountResult(int count);

		void getMsgCountResult(int count);
	}

	private View mView;

	@Override
	public void attach(View view) {
		mView = view;
		mModel = new PersonCenterModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);

	}

	/**
	 * 获取未读消息数目
	 */
	void getMsgCount() {
		execute(PersonCenterModel.MSG_GET_MSG_COUNT);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getMsgCountResult(PersonCenterModel.MsgResult msgResult) {
		if (isResponseSuccess(msgResult)) {
			mView.getMsgCountResult(msgResult.getContent().count);
		} else {
			mView.getMsgCountResult(msgResult.getContent().count);
		}
	}

	/**
	 * 查询余额
	 */
	void checkBalance() {
		execute(PersonCenterModel.MSG_CHECK_BALANCE);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getBalanceResult(PersonCenterModel.BalanceResult result) {
		if (isResponseSuccess(result)) {
			mView.getBalanceResult(result.getContent().staywithdraw, result.getContent().bank, result.getContent().cardId);
		} else {
			mView.getBalanceResult(View.UNKNOWN_MONEY_AMOUNT, "", "");
		}
	}

	/**
	 * 获取绑定申请数目
	 */
	void getApplyCount() {
		CLog.i(TAG, "getApplyCount");
		execute(null, PersonCenterModel.MSG_GET_APPLY_COUNT);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getApplyCountResult(PersonCenterModel.ApplyCountResult result) {
		if (isResponseSuccess(result)) {
			mView.getApplyCountResult(Integer.valueOf(result.getContent().count));
		} else {
			mView.getApplyCountResult(0);
		}
	}
}
